<template>
  <el-dialog
    :title="!dataForm.secondaryId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="二手房标题" prop="secondaryTitle">
      <el-input v-model="dataForm.secondaryTitle" placeholder="二手房标题"></el-input>
    </el-form-item>
    <el-form-item label="二手房名称" prop="secondaryName">
      <el-input v-model="dataForm.secondaryName" placeholder="二手房名称"></el-input>
    </el-form-item>
    <el-form-item label="二手房大小" prop="secondarySize">
      <el-input v-model="dataForm.secondarySize" placeholder="二手房大小"></el-input>
    </el-form-item>
    <el-form-item label="二手房面积" prop="secondaryArea">
      <el-input v-model="dataForm.secondaryArea" placeholder="二手房面积"></el-input>
    </el-form-item>
    <el-form-item label="二手房朝向" prop="secondaryDirection">
      <el-input v-model="dataForm.secondaryDirection" placeholder="二手房朝向"></el-input>
    </el-form-item>
    <el-form-item label="二手房装修情况" prop="secondaryRenovate">
      <el-input v-model="dataForm.secondaryRenovate" placeholder="二手房装修情况"></el-input>
    </el-form-item>
    <el-form-item label="二手房楼层" prop="secondaryStorey">
      <el-input v-model="dataForm.secondaryStorey" placeholder="二手房楼层"></el-input>
    </el-form-item>
    <el-form-item label="二手房建龄" prop="secondaryEstablish">
      <el-input v-model="dataForm.secondaryEstablish" placeholder="二手房建龄"></el-input>
    </el-form-item>
    <el-form-item label="二手房价格" prop="secondaryPrice">
      <el-input v-model="dataForm.secondaryPrice" placeholder="二手房价格"></el-input>
    </el-form-item>
    <el-form-item label="二手房默认图片" prop="secondaryImg">
      <el-input v-model="dataForm.secondaryImg" placeholder="二手房默认图片"></el-input>
    </el-form-item>
    <el-form-item label="是否满5年 1满五年 0没满" prop="secondaryIsfive">
      <el-input v-model="dataForm.secondaryIsfive" placeholder="是否满5年 1满五年 0没满"></el-input>
    </el-form-item>
    <el-form-item label="是否是唯一 1是 0不是" prop="secondaryJustone">
      <el-input v-model="dataForm.secondaryJustone" placeholder="是否是唯一 1是 0不是"></el-input>
    </el-form-item>
    <el-form-item label="照片id" prop="secondaryImgid">
      <el-input v-model="dataForm.secondaryImgid" placeholder="照片id"></el-input>
    </el-form-item>
    <el-form-item label="地区id" prop="secondaryRegionid">
      <el-input v-model="dataForm.secondaryRegionid" placeholder="地区id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          secondaryId: 0,
          secondaryTitle: '',
          secondaryName: '',
          secondarySize: '',
          secondaryArea: '',
          secondaryDirection: '',
          secondaryRenovate: '',
          secondaryStorey: '',
          secondaryEstablish: '',
          secondaryPrice: '',
          secondaryImg: '',
          secondaryIsfive: '',
          secondaryJustone: '',
          secondaryImgid: '',
          secondaryRegionid: ''
        },
        dataRule: {
          secondaryTitle: [
            { required: true, message: '二手房标题不能为空', trigger: 'blur' }
          ],
          secondaryName: [
            { required: true, message: '二手房名称不能为空', trigger: 'blur' }
          ],
          secondarySize: [
            { required: true, message: '二手房大小不能为空', trigger: 'blur' }
          ],
          secondaryArea: [
            { required: true, message: '二手房面积不能为空', trigger: 'blur' }
          ],
          secondaryDirection: [
            { required: true, message: '二手房朝向不能为空', trigger: 'blur' }
          ],
          secondaryRenovate: [
            { required: true, message: '二手房装修情况不能为空', trigger: 'blur' }
          ],
          secondaryStorey: [
            { required: true, message: '二手房楼层不能为空', trigger: 'blur' }
          ],
          secondaryEstablish: [
            { required: true, message: '二手房建龄不能为空', trigger: 'blur' }
          ],
          secondaryPrice: [
            { required: true, message: '二手房价格不能为空', trigger: 'blur' }
          ],
          secondaryImg: [
            { required: true, message: '二手房默认图片不能为空', trigger: 'blur' }
          ],
          secondaryIsfive: [
            { required: true, message: '是否满5年 1满五年 0没满不能为空', trigger: 'blur' }
          ],
          secondaryJustone: [
            { required: true, message: '是否是唯一 1是 0不是不能为空', trigger: 'blur' }
          ],
          secondaryImgid: [
            { required: true, message: '照片id不能为空', trigger: 'blur' }
          ],
          secondaryRegionid: [
            { required: true, message: '地区id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.secondaryId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.secondaryId) {
            this.$http({
              url: this.$http.adornUrl(`/house/housesecondary/info/${this.dataForm.secondaryId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.secondaryTitle = data.houseSecondary.secondaryTitle
                this.dataForm.secondaryName = data.houseSecondary.secondaryName
                this.dataForm.secondarySize = data.houseSecondary.secondarySize
                this.dataForm.secondaryArea = data.houseSecondary.secondaryArea
                this.dataForm.secondaryDirection = data.houseSecondary.secondaryDirection
                this.dataForm.secondaryRenovate = data.houseSecondary.secondaryRenovate
                this.dataForm.secondaryStorey = data.houseSecondary.secondaryStorey
                this.dataForm.secondaryEstablish = data.houseSecondary.secondaryEstablish
                this.dataForm.secondaryPrice = data.houseSecondary.secondaryPrice
                this.dataForm.secondaryImg = data.houseSecondary.secondaryImg
                this.dataForm.secondaryIsfive = data.houseSecondary.secondaryIsfive
                this.dataForm.secondaryJustone = data.houseSecondary.secondaryJustone
                this.dataForm.secondaryImgid = data.houseSecondary.secondaryImgid
                this.dataForm.secondaryRegionid = data.houseSecondary.secondaryRegionid
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/housesecondary/${!this.dataForm.secondaryId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'secondaryId': this.dataForm.secondaryId || undefined,
                'secondaryTitle': this.dataForm.secondaryTitle,
                'secondaryName': this.dataForm.secondaryName,
                'secondarySize': this.dataForm.secondarySize,
                'secondaryArea': this.dataForm.secondaryArea,
                'secondaryDirection': this.dataForm.secondaryDirection,
                'secondaryRenovate': this.dataForm.secondaryRenovate,
                'secondaryStorey': this.dataForm.secondaryStorey,
                'secondaryEstablish': this.dataForm.secondaryEstablish,
                'secondaryPrice': this.dataForm.secondaryPrice,
                'secondaryImg': this.dataForm.secondaryImg,
                'secondaryIsfive': this.dataForm.secondaryIsfive,
                'secondaryJustone': this.dataForm.secondaryJustone,
                'secondaryImgid': this.dataForm.secondaryImgid,
                'secondaryRegionid': this.dataForm.secondaryRegionid
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
